home *** CD-ROM | disk | FTP | other *** search
/ Deutsche Edition 1 / Deutsche Edition 1.iso / amok / 051-060 / amok58 / realconversions2 / realconversions2.doc < prev    next >
Text File  |  1993-11-04  |  4KB  |  127 lines

  1. DEFINITION RealConversions2;
  2.  
  3. *********************************************************************
  4.  
  5. IMPORTENT: This module can be used for converting REAL- or
  6.            LONGREAL-numbers. This works, because the compiler allows
  7.            the conditional compiling:
  8.            Normal compiling gives a module for REAL-numbers. But if You
  9.            SET the variable LONGREAL, a module for LONGREALs results.
  10.  
  11.            Call the compiler from the CLI as following to generate a
  12.            module for LONGREALs:
  13.  
  14.            Oberon SET LONGREAL RealConversions2
  15.  
  16. *********************************************************************
  17.  
  18.   PROCEDURE DeleteSpaces(VAR str:ARRAY OF CHAR);
  19.  
  20.   PROCEDURE StringToReal(str:ARRAY OF CHAR;
  21.                          VAR x:Real):BOOLEAN;
  22.  
  23.   PROCEDURE RealToString(x:Real;
  24.                          VAR str:ARRAY OF CHAR;
  25.                          gs,nks:INTEGER;expo,left:BOOLEAN):BOOLEAN;
  26.  
  27.  
  28. The procedure StringToReal() converts a string to a Real-number.
  29. RealToString() converts a Real-number to a string.
  30.  
  31. The advantage of my procedures are, that the conversion is more
  32. accurate than that of the original module OberonV2.0.RealConversions.
  33. Furthermore i like the format of my RealToString() better.
  34.  
  35.  
  36. PROCEDURE StringToReal(str:ARRAY OF CHAR; VAR x:Real):BOOLEAN;
  37.  
  38. This is the procedure for converting a character-string to a
  39. real-number. The result is TRUE if the syntax of the string is as
  40. following:
  41.  
  42. digit = "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9".
  43. sign  = ["+"|"-"].
  44. int   = {digit}.
  45. real  = sign int ["." int] [("E"|"e") sign int].
  46.  
  47.  
  48. The string MUST NOT contain any spaces. If You want to use StringToReal()
  49. with strings that contain spaces, You have to call DeleteSpaces() before
  50. You call StringToReal().
  51. If the string contains a number which is greater as the REAL- or
  52. LONGREAL-range, the procedure returns FALSE.
  53.  
  54. Examples:
  55.  
  56. String                 Real-Number   Result
  57.  
  58. ""                     0.0           TRUE
  59. "1"                    1.0           TRUE
  60. "3.84"                 3.84          TRUE
  61. "-.34532"             -0.34532       TRUE
  62. "23."                  23.0          TRUE
  63. ".E"                   0.0           TRUE
  64. "-34.5E+12"           -3.45E13       TRUE
  65.  
  66. "3,4"                  ---           FALSE
  67. "--3"                  ---           FALSE
  68. "3.45.E3"              ---           FALSE
  69. "9.8E372"              ---           FALSE
  70.  
  71.  
  72. PROCEDURE RealToString(x:Real;
  73.                        VAR str:ARRAY OF CHAR;
  74.                        gs,nks:INTEGER;expo,left:BOOLEAN):BOOLEAN;
  75.  
  76. RealToString() converts x into a character-string. gs determines, how
  77. much significant digits the number should have. The following numbers
  78. have five significant digits:
  79.  
  80. 12345
  81. 123.45
  82. 1.2345E-13
  83. 0.000000054321
  84.  
  85. The leading zeros of the last number are not significant digits
  86. because 0.000000054321 = 5.4321E-8. The factor E-8 is only a result of
  87. the used unit of measurement and does not affect the accuracy of the
  88. number.
  89.  
  90. 0.012 m   =12E-3 m =  12 mm
  91.  
  92. The above lengths in meters or millimeters are equivalent, the
  93. accuracy is allways two digigs or 1/100 = 1%.
  94.  
  95. Sometimes it is usefull to restrict the number of digits behind the
  96. point. For example, if You calculate a price in DM or $, it is not
  97. usefull to get a result like 2.89546. With nks You can restrict the
  98. digits behind the decimalpoint. If nks=n, then the number of digits
  99. behind the point is allway equal or less than n. If x has more digits
  100. before the point than gs allows, the scientific notation is used. ( in
  101. this case 3.4E7 means 3.4 * 10^7 ) This notation is allways used, if
  102. expo=TRUE. If left=TRUE, the number is insertet at the left side of
  103. the string, otherwise at the right side. The number is allways
  104. roundet:
  105.  
  106.  123.453      ==>  123.45
  107.  123.456      ==>  123.46
  108.  -34.9973     ==> -35.00
  109.  
  110.  
  111. IF |x|< 1, then the number of significant digits is less than gs. The
  112. reason for this is that most people like 0.000 better than
  113. 7.894E-11. To get the correct value 7.894E-11, You can call
  114. RealToString() as following:
  115.  
  116. ok:=RealToString(number,str,4,4, ABS(x)<=1, left);
  117.  
  118. IF |x|<=1, the scintific notation is used.
  119.  
  120. The result of RealToSting() is TRUE, if the string is large
  121. enough to contain the number.
  122.  
  123.  
  124. Stefan Salewski,  August 14, 1991
  125.  
  126.  
  127.